package org.afun.oldhand.home.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import org.afun.oldhand.R;
import org.afun.oldhand.home.javaBean.ChannelItemBean;

import java.util.List;

public class ChannelAdapter extends BaseAdapter {
    private Context mContext;
    private List<ChannelItemBean> data;

    public ChannelAdapter(Context mContext, List<ChannelItemBean> data) {
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_channel, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.ivChannel.setImageResource(data.get(position).getResId());
        holder.tvChannel.setText(data.get(position).getChannel_name());

        return convertView;
    }

    class ViewHolder {
        private ImageView ivChannel;
        private TextView tvChannel;

        ViewHolder(@NonNull View itemView) {
            ivChannel = itemView.findViewById(R.id.iv_channel);
            tvChannel = itemView.findViewById(R.id.tv_channel);
        }
    }
}
